package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

/*    @Autowired
    private DishFlavorService dishFlavorService;*/

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void savaWithFlavor(DishDto dishDto) {



        //保存菜品的基本信息到菜品表dish
        dishMapper.insert(dishDto);

        //保存dishDto中的菜品口味数据到菜品口味表dish_flavor

        //因为dish_flavor表中的dishId字段没有数据，页面没有给，所以要从dish表中get
/*        Long dishId = dishDto.getId(); //实际是从DishDto的父类Dish中get到的
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到dish_flavor表中
        dishFlavorService.saveBatch(flavors); //可以保存集合*/



        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors != null && flavors.size() > 0){

            Long dishId = dishDto.getId();

            for (DishFlavor flavor :
                    flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }


    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //操作两张表，分两步       查询DishDto = Dish + DishFlavor

        //1.查询菜品基本信息，从dish表查
        Dish dish = dishMapper.selectById(id);

        //3.把dish里面的基本信息拷贝到dishDto中
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //2.查询当前菜品对应的口味信息，从dish_flavor表查
        //条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //条件
        queryWrapper.eq(DishFlavor::getDishId,dish.getId()); //根据dishId从flavor表中查对应的菜品口味，传去dishId
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(queryWrapper);

        //4.把菜品对应的口味信息也添加到dishDto中
        dishDto.setFlavors(dishFlavors);


        return dishDto;
    }


    /**
     * 修改菜品
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        // 1、修改菜品dish表的数据
        dishMapper.updateById(dishDto);


        //取出菜品id Long dishId = dishDto.getId();
        // 2、修改菜品的口味数据 如何完成？？
        // 2.1 先删除原有的口味数据: delete from dish_flavor where dish_id = ?
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorMapper.delete(queryWrapper);


        // 2.2 添加菜品口味数据 List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> flavors = dishDto.getFlavors();

        if (flavors != null && flavors.size() > 0){

            Long dishId = dishDto.getId();

            for (DishFlavor flavor :
                    flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }



    }
}
